//
//  UpLoadImaeVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/27.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "UpLoadImaeVC.h"
#import "HDPicModle.h"
#import "GetPhoto.h"
#import "CJDateTime.h"
#import "XLImageViewer.h"

@interface UpLoadImaeVC (){
    UIImage *img;
    NSData *imageData1;
}
@property (weak, nonatomic) IBOutlet UIImageView *imgV;
@property (weak, nonatomic) IBOutlet UIButton *btnSave;

@end

@implementation UpLoadImaeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    [self setTitleName:CJLString(@"图片上传")];
    [self setBackLeftButton:@""];
    [self setTitleName:self.strTitle];

    self.btnSave.backgroundColor = [UIColor cjMainColor];
    self.btnSave.clipsToBounds = YES;
    self.btnSave.layer.cornerRadius = 5;
    
    // 不需要网络示例图片
    if ([CJControl getIsEmpty:self.strSampleNetWorkImg]) {
        
        if ([CJRegular getIsStrEmpty:self.strImgUrl]) {
            [self.btnSave setTitle:@"上传图片" forState:UIControlStateNormal];
            
            if ([CJRegular getIsStrEmpty:self.strSampleImg]) {
                 
            }else{
                self.imgV.image = [UIImage imageNamed:self.strSampleImg];
            }
        }else{
           
    //        if (self.isChangeImgV) {
               [self.btnSave setTitle:@"查看/修改认证图片" forState:UIControlStateNormal];
    //        }else{
    //            [self.btnSave setTitle:@"查看认证图片" forState:UIControlStateNormal];
    //        }
           [self.imgV sd_setImageWithURL:[NSURL URLWithString:self.strImgUrl] placeholderImage:[UIImage imageNamed:@"zcj_xiangji"]];

        }
    }else{
        
        // 已上传的 不在获取默认图
        if ([CJRegular getIsStrEmpty:self.strImgUrl]) {
            
            @weakify(self);
            [[HDNetworking sharedHDNetworking] GET:CJStringWithFormat(@"%@/ntocc-basic-api/preview/type/%@",CJBaseUrl,self.strSampleNetWorkImg) parameters:@{} success:^(id  _Nonnull responseObject) {
                
                @strongify(self);
                NSDictionary *dic = (NSDictionary *)responseObject;
                [self.btnSave setTitle:@"上传图片" forState:UIControlStateNormal];
                self.strSampleNetWorkImg = [CJControl getThreeUnaryValueStr:dic[@"img"]];
                [self.imgV sd_setImageWithURL:[NSURL URLWithString:self.strSampleNetWorkImg] placeholderImage:[UIImage imageNamed:@"zcj_xiangji"]];

            } failure:^(NSError * _Nonnull error) {
                
            }];
            
        }else{
            
            [self.btnSave setTitle:@"查看/修改认证图片" forState:UIControlStateNormal];
            [self.imgV sd_setImageWithURL:[NSURL URLWithString:self.strImgUrl] placeholderImage:[UIImage imageNamed:@"zcj_xiangji"]];
        }

    }
    
    [self setTapClickAction];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;

}

- (void)setTapClickAction{
    @weakify(self);
    [self.imgV setTapActionWithBlock:^{
        
        if ([CJRegular getIsStrEmpty:self.strSampleImg]) {
             
        }else{
//
//            DLog(@"==== %@",self.strSampleImg);
//            [[NSBundle mainBundle] pathForResource:@"2" ofType:@"png"]
            if ([CJRegular getIsStrEmpty:self.strImgUrl]) {
                
                if ([CJControl getIsEmpty:self.strSampleNetWorkImg]) {
                    [[XLImageViewer shareInstanse] showLocalImages:@[self.strSampleImg] index:0 fromImageContainer:self.imgV];
                }else{
                    [[XLImageViewer shareInstanse] showNetImages:@[self.strSampleNetWorkImg] index:0 fromImageContainer:self.imgV];
                }
                
            }else{
                [[XLImageViewer shareInstanse] showNetImages:@[self.strImgUrl] index:0 fromImageContainer:self.imgV];
            }
        }
    }];

}

- (void)selelctChangeImgVOrSeeBigPic{
    
    @weakify(self);
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *change = [UIAlertAction actionWithTitle:@"更换照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        @strongify(self);
        [self uploadImgV];

    }];
    UIAlertAction *seeBig = [UIAlertAction actionWithTitle:@"查看大图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        @strongify(self);
        [[XLImageViewer shareInstanse] showNetImages:@[self.strImgUrl] index:0 fromImageContainer:self.imgV];

    }];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [alertVC dismissViewControllerAnimated:NO completion:^{
            
        }];
    }];

    
//    if (self.isChangeImgV) {
        [alertVC addAction:change];
        [alertVC addAction:seeBig];
//    }else{
//        [alertVC addAction:seeBig];
//    }
    
//    if ([CJRegular getIsStrEmpty:self.strSampleImg]) {
//
//    }else{
//
//        UIAlertAction *sample = [UIAlertAction actionWithTitle:@"查看模板图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            [alertVC dismissViewControllerAnimated:NO completion:^{
//                [[XLImageViewer shareInstanse] showLocalImages:@[self.strSampleImg] index:0 fromImageContainer:self.imgV];
//            }];
//        }];
//        [alertVC addAction:sample];
//
//    }
    
    [alertVC addAction:cancle];

    [self presentViewController:alertVC animated:YES completion:nil];
    
}
- (IBAction)btnUpLoadImage:(id)sender {
    
//    if (IsNilOrNull(img) || IsNilOrNull(imageData1)) {
//        [CJAlert showStringDismissWithDelay:@"请先选择照片"];
//    }else{
//          [self upLoadImageUImage:img withNSData:imageData1];
//    }
    
     if ([CJRegular getIsStrEmpty:self.strImgUrl]) {
          [self uploadImgV];
     }else{
         [self selelctChangeImgVOrSeeBigPic];
     }

}

- (void)uploadImgV{
    @weakify(self);
    [GetPhoto getPhoto:self completion:^(UIImage *curImage, NSData *imageData) {
        
        @strongify(self);
        img = curImage;
        imageData1 = imageData;
        [self upLoadImageUImage:img withNSData:imageData1];
    }];
}


- (void)upLoadImageUImage:(UIImage *)img withNSData:(NSData *)imageData{
    
    NSString *strURL = @"";

    if ([CJControl isLogin]) {
        strURL = CJStringWithFormat(@"%@/ntocc-tps-api/fileUpload",CJBaseUrl);
    }else{
        NSString *strSalt = CJStringWithFormat(@"%@%@",[CJDateTime getCurrentTimesType:CJDateTimeModeYMDHAndString],[CJControl getDefaultSalt]);
        strURL = CJStringWithFormat(@"%@/ntocc-tps-api/fileUpload/%@",CJBaseUrl,[CJControl md5:strSalt]);

    }
    NSURL *str = [[NSURL alloc]initFileURLWithPath:[CJControl getImgFilePath:img]];

    NSDictionary *dic = @{
                          @"file":str
                          };
    HDPicModle *model = [[HDPicModle alloc]init];
    model.pic = img;
    model.picData = imageData;
    model.picName = @"file";
    model.fileName = [CJControl getImgFilePath:img];

    @weakify(self);
    [[HDNetworking sharedHDNetworking] POST:strURL parameters:dic andPic:model progress:^(NSProgress * _Nullable progress) {
         DLog(@"%lf", 1.0 * progress.completedUnitCount / progress.totalUnitCount);
    } success:^(id  _Nonnull responseObject) {
        
        @strongify(self);
        NSString *strStatus = CJStringWithFormat(@"%@",responseObject[@"status"]);
        
        if (strStatus.integerValue == 0) {
            
             self.imgV.image = img;
            if (self.blcokSuccee) {
                self.blcokSuccee(responseObject[@"data"]);
            }
            [self pop];
        }
        
        [CJAlert showStringDismissWithDelay:responseObject[@"msg"]];

    } failure:^(NSError * _Nonnull error) {
        DLog(@"+++++++%@",error);

    }];
}

@end
